package handler

import (
	"cargo-hub/cache"
	"cargo-hub/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CreateRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func UserRoleFind(c *gin.Context) {
	uid := c.Query("uid")
	var role []model.Role
	//SELECT
	//*
	//FROM
	//roles
	//LEFT JOIN user_roles ON roles.id=user_roles.rid
	//WHERE user_roles.uid=1
	err := model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id=user_roles.rid").
		Where("user_roles.uid=?", uid).
		Find(&role).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}

func RoleNodeFind(c *gin.Context) {
	rid := c.Query("rid")
	var node []model.Node
	//SELECT
	//*
	//FROM
	//nodes
	//LEFT JOIN role_nodes ON nodes.id=role_nodes.nid
	//WHERE role_nodes.rid=1
	err := model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id=role_nodes.nid").
		Where("role_nodes.rid=?", rid).
		Find(&node).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}

func CreateCarGo(c *gin.Context) {
	cargo_address := c.PostForm("cargo_address")
	cargo_name := c.PostForm("cargo_name")
	cargo_code := c.PostForm("cargo_code")
	cargo_type := c.PostForm("cargo_type")
	num, _ := strconv.Atoi(c.PostForm("num"))
	data := model.CarGo{
		CargoAddress: cargo_address,
		CarGOName:    cargo_name,
		CarGoCode:    cargo_code,
		CarGOType:    cargo_type,
		Num:          num,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return

	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CreateOrder(c *gin.Context) {
	OrderCode := c.PostForm("order_code")
	OrderStatus := c.PostForm("order_status")
	OrderName := c.PostForm("order_name")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	data := model.Order{
		OrderCode:   OrderCode,
		OrderStatus: OrderStatus,
		OrderName:   OrderName,
		Cid:         cid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return

	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func ListOrderCargo(c *gin.Context) {
	var list []model.OrderCarGoList
	redis_key := "list_"
	CacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if CacheData == "" {
		//SELECT
		//orders.id AS OrderID,
		//car_gos.cargo_address AS CargoAddress,
		//car_gos.car_go_name AS CarGOName,
		//car_gos.car_go_code AS CarGoCode,
		//car_gos.car_go_type AS CarGOType,
		//car_gos.num AS  Num,
		//orders.order_code AS OrderCode,
		//orders.order_name AS OrderName,
		//orders.order_status AS OrderStatus
		//FROM
		//car_gos
		//LEFT JOIN orders ON car_gos.id=orders.cid
		model.DB.Table("car_gos").Select("orders.id AS OrderID",
			"car_gos.cargo_address AS CargoAddress",
			"car_gos.car_go_name AS CarGOName",
			"car_gos.car_go_code AS CarGoCode",
			"car_gos.car_go_type AS CarGOType",
			"car_gos.num AS  Num",
			"orders.order_code AS OrderCode",
			"orders.order_name AS OrderName",
			"orders.order_status AS OrderStatus",
		).Joins("LEFT JOIN orders ON car_gos.id=orders.cid").Find(&list)
		jsData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(CacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": list,
	})
	return
}

func DetailOrderCargo(c *gin.Context) {
	id := c.Query("id")
	var list model.OrderCarGoList
	redis_key := "detail" + id
	CacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if CacheData == "" {
		//SELECT
		//orders.id AS OrderID,
		//car_gos.cargo_address AS CargoAddress,
		//car_gos.car_go_name AS CarGOName,
		//car_gos.car_go_code AS CarGoCode,
		//car_gos.car_go_type AS CarGOType,
		//car_gos.num AS  Num,
		//orders.order_code AS OrderCode,
		//orders.order_name AS OrderName,
		//orders.order_status AS OrderStatus
		//FROM
		//car_gos
		//LEFT JOIN orders ON car_gos.id=orders.cid
		model.DB.Table("car_gos").Select("orders.id AS OrderID",
			"car_gos.cargo_address AS CargoAddress",
			"car_gos.car_go_name AS CarGOName",
			"car_gos.car_go_code AS CarGoCode",
			"car_gos.car_go_type AS CarGOType",
			"car_gos.num AS  Num",
			"orders.order_code AS OrderCode",
			"orders.order_name AS OrderName",
			"orders.order_status AS OrderStatus",
		).Joins("LEFT JOIN orders ON car_gos.id=orders.cid").
			Where("orders.id=?", id).Find(&list)
		jsData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(CacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": list,
	})
	return
}
